import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        classIds: null,
        name: null,
        or: true
    }
}

const initTraineePage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}

const state = {
    data: {
        trainList: [],
        learnList: [],
        deparList: [],
        traineeList: [],
        trainingsByTrainee:[]
    },
    ui: {
        page: deepCopy(initPage),
        pageByTrainee: deepCopy(initTraineePage)
    }
}

// getters
const getters = {
    trainList: state => state.data.trainList,
    learnList: state => state.data.learnList,
    deparList: state => state.data.deparList,
    traineeList: state => state.data.traineeList,
    trainingsByTrainee: state => state.data.trainingsByTrainee
}

// actions
const actions = {

    getTrainList({ state, commit }, data) {
        api.gettrainTask(data ? data : state.ui.page, {
            loading: true,
            success: res => {
                const { pages, total } = res
                commit('SETANSWERPAGE', { pages, total })
                commit('setTrainList', res)
            }
        })
    },

    removeonedatatrain({ dispatch }, data) {
        api.removeonetrain(
            data,
            {
                success: () => {
                    dispatch('getTrainList')
                }
            }
        )
    },

    getLearnList({ commit }, data) {
        api.getstudyTask(data ? data : state.ui.page, {
            success: res => {
                commit('SETLEARNLIST', res.content)
            }
        })
    },

    getDeparList({ commit }, data) {
        api.getDepartements({
            method: 'get',
            data: data ? data : state.ui.page,
            success: (res) => {
                commit('SETDEPARLIST', res)
            }
        })
    },

    getTraineeList({ commit }, data) {
        api.getTrainees({
            method: 'post',
            data: data ? data : state.ui.page,
            success: (res) => {
                commit('SETTRAINEELIST', res.content ? res.content : [])
            }
        })
    },

    addTrain({ dispatch }, data) {
        api.addTrain(data.data, {
            success: (res) => {
                dispatch('getTrainList')
                data.cb && data.cb(res)
            }
        })
    },

    editTrain({ dispatch }, data) {
        api.editTrain(data, {
            success: (res) => {
                dispatch('getTrainList')
            }
        })
    },

    addLearnRelation({ dispatch }, data) {
        api.addLearnRelation(data, {
            success: (res) => {
                dispatch('getTrainList')
            }
        })
    },

    addTrineeRelation({ dispatch }, data) {
        api.addTrineeRelation(data, {
            success: (res) => {
                dispatch('getTrainList')
            }
        })
    },
    getTrainingsByTrainee({ commit }, page) {
        api.getTrainingsByTrainee(page ? page : state.ui.pageByTrainee, {
            success: res => {
                const { pages, total } = res
                commit('setTrainingsByTraineePage', {
                    pages,
                    total
                })
                commit('setTrainingsByTrainee', res)
            }
        })
    }
}

// mutations
const mutations = {
    setTrainingsByTrainee(state, payload) {
        state.data.trainingsByTrainee = payload.content
    },
    setTrainingsByTraineePage(state, page) {
        page && setPage(state.ui.pageByTrainee, page)
    },
    setTrainList: (state, payload) => {
        state.data.trainList = payload
    },

    SETLEARNLIST(state, payload) {
        state.data.learnList = payload
    },

    SETDEPARLIST(state, payload) {
        state.data.deparList = payload
    },

    SETTRAINEELIST(state, payload) {
        state.data.traineeList = payload
    },

    SETATASKPAGE(state, page) {
        page && setPage(state.ui.page, page)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
